(function() { 
	appCommon.controller('postCtr', postCtroller);	 

	function postCtroller($rootScope, $scope, $http, $httpParamSerializer, ngDialog) {
        $rootScope.view = ""; 

        $http({
            method: 'POST',
            url: '/index.php?m=home&a=getType&c=Post',            
            headers:{'Content-Type': 'application/x-www-form-urlencoded'}
        }).then(function successCallback(response) {
            var data = response.data;
            if(data.status === 0) {
                //未登录
                window.location.href="/index.php/Home/#/login";                
            }else if(data.status === 1){         
                //已经登录
                window.gEdit = UE.getEditor('editor');
                $scope.post_type_list = data.type;
            }else if(data.status === 2){        
                //        
                $scope.post_type_list = [];  
            }
        }, function errorCallback(response) {
            console.log(response)
        });

		$scope.save = function() {
            var post_content = gEdit.getContent();
            if(!$scope.post_title) {
                $scope.postTitleError = "请填写文章标题！";
            }else if(!$scope.post_type) {
                var dialog = ngDialog.open({      
                    plain: true,              		
                    template: '<div class="save-massage">请选择发表类型</div>', 
                    controller: 'userInfoCtr'
                });      
            }else if(!post_content) {
                var dialog = ngDialog.open({      
                    plain: true,              		
                    template: '<div class="save-massage">请输入发表内容</div>', 
                    controller: 'userInfoCtr'
                });      
            }else {
                if(post_content.match(/http.*/) && post_content.match(/img/)) {
                    var post_image = post_content.match(/http.*/)[0].split('" title')[0];
                }else {
                    var post_image = "";
                }                

                $http({
                    method: 'POST',
                    url: '/index.php?m=home&a=getPost&c=Post',
                    data: $httpParamSerializer({
                        'post_title': $scope.post_title,
                        'post_type': $scope.post_type,
                        'post_content': post_content,
                        'post_text': gEdit.getContentTxt(),
                        'post_image': post_image
                    }),
                    headers:{'Content-Type': 'application/x-www-form-urlencoded'}
                }).then(function successCallback(response) {
                    var data = response.data;
                    if(data.status === 1) {
                        window.location.href="/index.php"
                    }else if(data.status === 0){
                        if(data.type === "userNameError") {
                            $scope.userNameError = "show";
                            $scope.passwordError = "";
                            $scope.userNameMessage = data.message;
                        }else if(data.type === "passwordError") {
                            $scope.userNameError = "";
                            $scope.passwordError = "show";
                            $scope.passwordMessage = data.message;
                        }
                    }
                }, function errorCallback(response) {
                    console.log(response)
                });
            }
        }
	}
})();